﻿using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using ModernHttpClient;
using MyCard.Services;
using MyCard.Services.Utils.HttpClient;
using Newtonsoft.Json;
using Xamarin.Forms;

namespace LiveMakkah.Services.HttpClient
{
    public class HttpClientFactory : IHttpClientFactory
    {
        public HttpMessageHandler DefaultHandler { get; set; }

        public HttpMessageHandler Handler
        {
            get
            {
                return HttpClientHandlerFactory != null ? HttpClientHandlerFactory() : new HttpClientHandler();
            }
        }

        public System.Net.Http.HttpClient Get()
        {
            return Get(Handler);
        }

        public System.Net.Http.HttpClient Get(HttpMessageHandler handler)
        {
            if (handler == null) throw new ArgumentNullException("handler");
            var client = new System.Net.Http.HttpClient(handler);

        /*    var deviceService = Mvx.Resolve<IDeviceManager>();
            var locale = deviceService.GetLocale();*/
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
          /*  client.DefaultRequestHeaders.AcceptLanguage.Add(new StringWithQualityHeaderValue(locale));
           
            var profileManager = Mvx.Resolve<IProfileManager>();
            if (profileManager != null && profileManager.Credentials != null &&
                profileManager.Credentials.AccessToken != null){
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", profileManager.Credentials.AccessToken);
            }
            */

            return client;
        }

        public HttpClientHandlerFactory HttpClientHandlerFactory { private get; set; }
    }
}
